﻿using System;
using aier.xrm.wcf;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using aier.dataaccess.model;
using aier.business.logic;
using System.Web.Hosting;
using System.Security.Cryptography;
using Microsoft.Xrm.Sdk.Messages;

namespace aier.api.unitest
{
    [TestClass]
    public class XrmServiceUT
    {
        private XrmService xrmService = null;

        [TestInitialize]
        public void Init()
        {
            xrmService = new XrmService();
        }

        [TestMethod]
        public void GetOrganizationServiceProxyUT()
        {
            WhoAmIRequest whoAmI = new WhoAmIRequest();

            xrmService.UseOrganizationServiceProxy("AIERCHINA\\administrator", "pass@word1", action =>
            {
                var resp = (WhoAmIResponse)action.Execute(whoAmI);
            });
        }

        [TestMethod]
        public void GetOnLineOrganizationServiceProxyUT()
        {
            WhoAmIRequest whoAmI = new WhoAmIRequest();
            xrmService.UseOrganizationServiceProxy("yanfang@yanfangzhang.onmicrosoft.com", "BUPTzyf110610", action =>
            {
                var resp = (WhoAmIResponse)action.Execute(whoAmI);
            });
        }

        [TestMethod]
        public void UseParallelOrganizationServiceManagerUT()
        {
            IDictionary<string, OrganizationRequest> keyValuePairs = new Dictionary<string, OrganizationRequest>();

            var whoamiReq1 = new KeyValuePair<string, OrganizationRequest>("whoamiReq1", new CreateRequest());
            var whoamiReq2 = new KeyValuePair<string, OrganizationRequest>("whoamiReq2", new WhoAmIRequest());
            keyValuePairs.Add(whoamiReq1);
            keyValuePairs.Add(whoamiReq2);


            xrmService.UseParallelOrganizationServiceManager(t =>
            {
               var resp = t.ParallelProxy.Execute(keyValuePairs,(req,error)=> {

                });
                
            });
        }

        [TestMethod]
        public void TestCreateCommunityById()
        {
            Community community = new Community();
            community.new_name = "测试社区";
            community.new_province = Guid.Parse("FB83797C-E3C1-E411-80B6-00155D94EE04");
            community.new_city = Guid.Parse("C8D442FC-E7C1-E411-80B6-00155D94EE04");
            community.new_county = Guid.Parse("61E0084D-89C2-E411-80B6-00155D94EE04");
            community.ownerId = Guid.Parse("0268B153-33AA-E411-80B6-00155D94EE04");


            CommunityBll communityBll = new CommunityBll();
            communityBll.CreateCommuntity(community);
        }

        [TestMethod]
        public void TestPageObject()
        {
            HomeBll homeBll = new HomeBll();

            //for(int i=1;i < 200;i++)
            //{
                var ss1 = homeBll.QueryHomeInfoByPagin(Guid.Parse("AA9AC383-4504-E511-80BD-00155D94EE04"), 1, 20,"desc");
            //}
        }

        [TestMethod]
        public void HostingEnvironmentUT()
        {
            RandomNumberGenerator cryptoRandomDataGenerator = new RNGCryptoServiceProvider();
            byte[] buffer = new byte[50];
            cryptoRandomDataGenerator.GetBytes(buffer);
            var refreshTokenId = Convert.ToBase64String(buffer).TrimEnd('=').Replace('+', '-').Replace('/', '_');
        }

    }
}
